package cn.edu.henu.team.fragments;

import android.widget.ScrollView;
import android.widget.Toast;

import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.edu.henu.team.R;
import cn.edu.henu.team.adapter.CircleUserAdapter;
import cn.edu.henu.team.adapter.HotUserAdapter;
import cn.edu.henu.team.base.BaseFragment;
import cn.edu.henu.team.bean._User;
import cn.edu.henu.team.bean.familyCircle;
import cn.edu.henu.team.bean.the_collection;

import static cn.bmob.v3.Bmob.getApplicationContext;

public class fragment2_circle extends BaseFragment{
    private List<the_collection> data = new ArrayList<>();//必须初始化
    private RecyclerView recyclerView;
    private CircleUserAdapter adapter;
    private String[] ids;
    private SmartRefreshLayout refreshSrl;
    private ScrollView scrollView;


    private _User user = BmobUser.getCurrentUser(_User.class);

    public fragment2_circle() {}

    public static fragment2_circle newInstance() {
        fragment2_circle fragment = new fragment2_circle();
        return fragment;
    }

    @Override
    protected void clickEvents() {

    }

    @Override
    protected int initLayout() {
        return R.layout.fragment_2_news;
    }

    @Override
    protected void initView() {
        refreshSrl=mRootView.findViewById(R.id.refresh_srl);
        refreshSrl.setRefreshHeader(new ClassicsHeader(getApplicationContext()));
        refreshSrl.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refreshSrl.finishRefresh(1000/*,false*/);
                data.clear();
                initData();
            }
        });
        recyclerView = mRootView.findViewById(R.id.recyclerview);
    }

    @Override
    protected void initData() {
        // 查询当前用户的家庭圈友
        BmobQuery<familyCircle> query = new BmobQuery("familyCircle");
        query.addWhereEqualTo("currentUserID",user.getObjectId());
        query.order("-createdAt");
        query.findObjects(new FindListener<familyCircle>() {
            @Override
            public void done(List<familyCircle> list, BmobException e) {
                if (e == null) {
                    ids = new String[list.size()];
                    if (list.size() != 0) {
                        for (int i = 0; i < list.size(); i++) {
                            if(list.get(i).getOtherUserID().toString()!=null || list.get(i).getOtherUserNickName().toString() != null || list.get(i).getImageUrl() !=null){
                                the_collection flag = new the_collection(list.get(i).getOtherUserID(), list.get(i).getOtherUserNickName(), list.get(i).getImageUrl());
                                flag.setObjectId(list.get(i).getObjectId());
                                data.add(flag);
                            }
                        }
                        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
                        recyclerView.setLayoutManager(layoutManager);
                        adapter = new CircleUserAdapter(data,getActivity());
                        recyclerView.setAdapter(adapter);
                    }
                } else {
                    Toast.makeText(getContext(), "用户查询失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}